package com.luoye.lj.config;

import com.luoye.lj.pojo.vo.Message;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.luoye.lj.pojo.vo.GlobalExceptionEnum.*;

@Slf4j
//@ControllerAdvice
//@ResponseBody
public class GlobalExceptionHandler {
    //private Message res=new Message();//返回结果包装类
    @Autowired(required = false)
    private HttpServletRequest request;

    /**
     * SQL 异常
     */
    @ExceptionHandler(SQLException.class)
    public Message handleSQLException(SQLException e) {
        log.info("请求接口：{},SQLException异常：{}", request.getRequestURL(), e.getMessage());
        return new Message().error(SQL_EXCEPTION.getCode(), e.getMessage());
    }

    /**
     * 处理请求参数DTO 校验异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Message handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        Map<String, Object> errorMap = new HashMap<>(16);
        for (FieldError fieldError : fieldErrors) {
            errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
        }
        log.info("请求接口：{},MethodArgumentNotValidException异常：{}", request.getRequestURL(), errorMap);
        return new Message().error(PARAMETER_CHECK_EXCEPTION.getCode(), PARAMETER_CHECK_EXCEPTION.getMessage());
    }

    /**
     * 处理feign异常
     */
    @ExceptionHandler(FeignException.class)
    public Message handleFeignException(FeignException e) {
        log.info("请求接口：{},FeignException异常：{}", request.getRequestURL(), e);
        return new Message().error(FEIGN_CALL_EXCEPTION.getCode(), e.getMessage());
    }
    //-----------------------------------------------拦截容易出现的异常------------------------------------------------

    /**
     * 空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    public Message handleNullPointerException(NullPointerException e) {
        log.info("请求接口：{},NullPointerException异常：{}", request.getRequestURL(), e.getMessage());
        return new Message().error(NULL_POINTER_EXCEPTION.getCode(), e.getMessage());
    }

    /**
     * 数组越界异常
     */
    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Message handleArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e) {
        log.info("请求接口：{},ArrayIndexOutOfBoundsException异常：{}", request.getRequestURL(), e.getMessage());
        return new Message().error(ARRAY_OUT_OF_BOUNDS_EXCEPTION.getCode(), e.getMessage());
    }

    /**
     * StringIndexOutOfBoundsException
     */
    @ExceptionHandler(StringIndexOutOfBoundsException.class)
    public Message handleStringIndexOutOfBoundsException(StringIndexOutOfBoundsException e) {
        log.info("请求接口：{},StringIndexOutOfBoundsException异常：{}", request.getRequestURL(), e.getMessage());
        return new Message().error(STRING_OUT_OF_BOUNDS_EXCEPTION.getCode(), e.getMessage());
    }


    /**
     * Arithmetic
     */
    @ExceptionHandler(ArithmeticException.class)
    public Message ArithmeticExceptionException(ArithmeticException e) {
        log.info("请求接口：{},ArithmeticException异常：{}", request.getRequestURL(), e.getMessage());
        return new Message().error(Arithmetic_EXCEPTION.getCode(), e.getMessage());
    }


}
